package cn.hfatec.shms.web.xc.xcJjbmx.action;

import cn.hfatec.shms.web.xc.xcTjmxb.model.XcTjmxbParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.xc.xcJjbmx.model.XcJjbmxParams;
import cn.hfatec.shms.web.xc.xcJjbmx.model.XcJjbmx;
import cn.hfatec.shms.web.xc.xcJjbmx.service.XcJjbmxService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/xc/xcJjbmx")
public class XcJjbmxAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XcJjbmxService")
    private XcJjbmxService xcJjbmxService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(XcJjbmxParams xcJjbmxParams, Page page, Model model) {

        Table table = xcJjbmxService.queryXcJjbmxTable(xcJjbmxParams, page);
        model.addAttribute("xcJjbmxsTable", table);
        model.addAttribute("xcJjbmxParams", xcJjbmxParams);
        return "/xc/xcJjbmx/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XcJjbmxParams xcJjbmxParams, Page page, Model model) {
        try {
            list(xcJjbmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbmx/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            XcJjbmx xcJjbmx = new XcJjbmx();
            model.addAttribute("xcJjbmx", xcJjbmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            XcJjbmx xcJjbmx = xcJjbmxService.getXcJjbmx(id);
            model.addAttribute("xcJjbmx", xcJjbmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbmx/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXcJjbmx(Integer id) {
        try {
            xcJjbmxService.deleteXcJjbmx(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + id + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXcJjbmx(@Valid XcJjbmx xcJjbmx, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            xcJjbmxService.saveXcJjbmx(xcJjbmx);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据XcJjbmx保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/dorefreshJjbmx")
    @ResponseBody
    public String dorefreshJjbmx(XcJjbmxParams xcJjbmxParams, Page page, Model model) {

        try {
            list(xcJjbmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbmx/selectJjbmx", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(XcJjbmxParams xcJjbmxParams, Model model, Page page) {
        try {
            list(xcJjbmxParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbmx/selectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
